/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.NetworkManagement;

import Utilities.Functions;
import Utilities.PermissionsConstants;

/**
 * Instances of this class are used by the user interface when a request to
 * modify an internal network is sent to the networks management service.
 *
 * @author Angel Sanadinov
 */
public class ModifyInternalNetworkRequest extends NetworkManagementRequest
{
    private String newExternalName;         //new external name
    private String permissions;             //new permissions
    private boolean isNewExternalNameSet;   //denotes whether the external name was set
    private boolean arePermissionsSet;      //denotes whether the permissions were set

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     */
    public ModifyInternalNetworkRequest()
    {
        super();
        this.newExternalName = null;
        this.isNewExternalNameSet = false;
        this.arePermissionsSet = false;
        setRequestedAction(PermissionsConstants.ACTION_NETWORK_MODIFY_INTERNAL_NET);
    }

    /**
     * Constructs a "modify internal network" request with the specified data.
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the network will reside
     * @param networkId the id of the internal network
     */
    public ModifyInternalNetworkRequest(int requestorId, int serverId, String networkId)
    {
        this();
        setRequestorId(requestorId);
        setServerId(serverId);
        setNetworkId(networkId);
    }

    /**
     * Retrieves the new external network name.
     *
     * @return the external name
     */
    public String getNewExternalName()
    {
        return newExternalName;
    }

    /**
     * Retrieves the new permissions of the network.
     *
     * @return the network's permissions
     */
    public String getPermissions()
    {
        return permissions;
    }

    /**
     * Returns whether a new external name was set.
     *
     * @return <code>true</code> if a new name was set or <code>false</code> otherwise
     */
    public boolean isNewExternalNameSet()
    {
        return isNewExternalNameSet;
    }

    /**
     * Returns whether new permissions data was set.
     *
     * @return <code>true</code> if new permissions data was set or <code>false</code>
     *         otherwise
     */
    public boolean arePermissionsSet()
    {
        return arePermissionsSet;
    }

    /**
     * Sets new permissions for the internal network.
     *
     * @param permissions the new permissions data
     */
    public void setPermissions(String permissions)
    {
        this.permissions = permissions;
        arePermissionsSet = true;
    }

    /**
     * Sets a new external name for the network.
     *
     * @param newExternalName the new external name
     */
    public void setNewExternalName(String newExternalName)
    {
        this.newExternalName = newExternalName;
        isNewExternalNameSet = true;
    }

    @Override
    public boolean isValid()
    {
        if(super.isValid() && isNetworkIdValid()
                && (!arePermissionsSet || Functions.arePermissionsValid(permissions))
                && (!isNewExternalNameSet || (newExternalName != null && !newExternalName.trim().equals(""))))
            return true;
        else
            return false;
    }

    /**
     * Returns a textual representation of the object. <br>
     *
     * It is in the form: "ClassName: (networkId,newExternalName,permissions)".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        return ModifyInternalNetworkRequest.class.getSimpleName()
                + ": (" + getNetworkId() + "," + newExternalName + "," + permissions + ")";
    }
}
